﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Volo.Abp;
using Volo.Abp.Authorization;
using Volo.Abp.DependencyInjection;

namespace abp
{
    [Dependency(ReplaceServices = true)]
    public class AuthorizationService: IAbpAuthorizationService,ITransientDependency
    {
        public async Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object resource, IEnumerable<IAuthorizationRequirement> requirements)
        {
//            throw new NotImplementedException();


            var userId = user.FindFirst(a => a.Type == ClaimTypes.NameIdentifier);
            if (userId == null) throw new UserFriendlyException("用户未登入");
            //todo 校验mogodb是否登入



            Debug.WriteLine("测试授权");

            return await Task.Run(() => AuthorizationResult.Success());
        }

        public async Task<AuthorizationResult> AuthorizeAsync(ClaimsPrincipal user, object resource, string policyName)
        {
//            throw new NotImplementedException();

            Debug.WriteLine("测试授权");
            return await Task.Run(() => AuthorizationResult.Success());
        }

        public IServiceProvider ServiceProvider { get; }
        public ClaimsPrincipal CurrentPrincipal { get; }
    }
}
